Redstone Powered Light Block
This is an example of medium complexity showing how to make a "Lamp" block that can be activated or deactivated via redstone current. It showcases the basics of the activated/deactivated attributes, as well as how to change a block via a simple script. Also, it lights stuff up so you can control monster spawning better. No pistons required! mod.js config.addBlockIdProperty("lampBlockID"); mod.addBlock("lampBlock.js", "normal"); lampBlocks.js name = "lampBlock"; id = config.getBlockId("lampBlockID"); displayName0 = "Lamp Block"; material = "glass"; stepSound = "glass"; opacity = 0; transparent = true; tileTransparent = true; creativeTab = "decorations"; addToCreative0 = true; addToCreative1 = false; hardness0 = 1; resistance0 = 5; light0 = 0; drop0 = config.getBlockId("lampBlockID") + ":0 0"; onRedstoneSignal0 = "if(redstoneSignal true)\ {world.setBlockMetadata(position, 1);}"; hardness1 = 1; resistance1 = 5; light1 = 15; drop1 = config.getBlockId("lampBlockID") + ":0 0"; onRedstoneSignal1 = "if(redstoneSignal false)\ {world.setBlockMetadata(position, 0);}"; texturefile = "example.png"; textureIndexXP0 = 0; textureIndexXN0 = 0; textureIndexYP0 = 0; textureIndexYN0 = 0; textureIndexZP0 = 0; textureIndexZN0 = 0; textureIndexXP1 = 33; textureIndexXN1 = 33; textureIndexYP1 = 33; textureIndexYN1 = 33; textureIndexZP1 = 33; textureIndexZN1 = 33; ---- Basics To start with, you can set up the basics of the block like you would any other Normal block. You can adjust things to your liking including the name, the material the block is made of, and of course whether it is transparent or not. For this example, we'll be using the one shown in the image above which is made from glass and is transparent when not lit. name="Lamp Block"; id=220; texturefile="example.png"; textureindex=0; type="normal"; material="glass"; stepsound="glass"; hardness=1; resistance=5; iddropped=220; opacity=0; //An opaque block will block its own light. transparency=true; light=0; //This must be set for the block to be unlit when placed. Damage Values Creating a block like this using only a single block ID is a matter of best practice. As such, we'll be setting up this block to change itself to a different damage value. In order to do that, the following attributes must be added to the block file in order to define the non-default values. damagevalues=1; //This shows we have one value above the default. name1="Lamp Block"; light1=13; //This is the light value of the 'on' state of the block textureindex1=33; //The 'on' state uses a different texture in this example damagedropped1=0; //This tells the block to always drop its 'off' state Switch Attributes Now that both the on and off states of the block are clearly defined, the only thing remaining is to add a mechanism for switching between them. For this, the "activated" and "deactivated" triggers are applied. Each is set to change the block to its opposite form. activated="world.setBlockIdAndMetadata(origin, 220, 1);"; This is the version that the game looks for when the block is in its 'off' state. It tells the game that when the 'off' light block gets redstone current it needs to change the block at Origin (where the block itself is) to ID 220 (itself), damage value #1 (the 'on' state). The reason why setBlockIdAndMetadata is used in this example is because the only way to get the game to update lighting is to update the block. Simply changing the damage value won't alter the lighting. On the other hand, if you're adapting this concept to a block that merely changes textures when activated you can use setBlockMetadata without issue. With the activated attribute in place, there needs to be a corresponding trigger to turn the light block off when the redstone current is removed. deactivated1="world.setBlockIdAndMetadata(origin, 220, 0);"; Note that the attribute is "deactivated'1'" and not simply "deactivated". This is because it's the 'on' state (damage value 1) that needs to be in charge of turning itself off when it is denied redstone current. The actual variables in this one are set up exactly the same as in the other example, just from the other side of the on/off dichotomy. Category:Examples Category:Scripting Category:Examples (CS1) Category:Examples (CS2) Category:Custom Stuff 2